package com.jintian.smart.kernel.core.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
import com.jintian.smart.kernel.core.domain.ITailEntity;

import java.io.IOException;

public class TailEntityDeserializer extends BeanDeserializer {
	private static final long serialVersionUID = 1L;

	public TailEntityDeserializer(BeanDeserializerBase src) {
		super(src);
	}

	@Override
	protected void handleUnknownVanilla(JsonParser p, DeserializationContext ctxt, Object beanOrBuilder,
			String propName) throws IOException {
		if (beanOrBuilder instanceof ITailEntity) {
			ITailEntity tail = (ITailEntity) beanOrBuilder;
			Class<?> valueClazz = tail.getExtAttrClass(propName);
			if (valueClazz == null) {
				valueClazz = Object.class;
			}
			Object value = p.readValueAs(valueClazz);
			((ITailEntity) beanOrBuilder).set(propName, value);
			return;
		}
		super.handleUnknownVanilla(p, ctxt, beanOrBuilder, propName);
	}
}
